
# -*- coding: UTF-8 -*-
# @File: testCalculator.py
# @Author: fy
# @date: 2021/8/23 17:51

import pytest
import yaml
import os
from Functions.Calculator import Calculator
import allure
import logging

# 注意 Calculator2.yaml  Calculator3.yaml获取数据时正确的
def getYamlData(find_key)->list:
    dict_datas = {}
    list_datas = []
    with open("../datas/Calculator.yaml") as f:
        data = yaml.safe_load(f)
        # print(data)
        dict_datas = data[0].get('datas').get(find_key)

    for key in dict_datas:
        # print(dict_datas[key])
        for value in dict_datas[key]:
            value = value.split(',')
            list_datas.append(value)
    return list_datas

def getData_1(find_key)->list:
    dict_datas = {}
    list_datas = []
    print(os.getcwd())
    # data_file_name = str(os.path.dirname(os.getcwd())) + "/datas/Calculator2.yaml"
    data_file_name = str(os.getcwd()) + "/datas/Calculator2.yaml"
    print(data_file_name)
    with open(data_file_name) as f:
        data = yaml.safe_load(f)
        dict_datas = data[0].get('datas').get(find_key)
        # print(dict_datas)
        # return dict_datas

    for key in dict_datas:
        print(dict_datas[key])
        list_datas += dict_datas[key]
    print(list_datas)
    return list_datas


@pytest.mark.skip
def testYamlData_1():
    # print(getData_1('div'))
    # print(type(getData_1('div')))
    getData_1('div')



def getData_2(find_key)->list:
    dict_datas = {}
    list_datas = []
    with open("../datas/Calculator3.yaml") as f:
        data = yaml.safe_load(f)
        dict_datas = data[0].get('datas').get(find_key)
        # print(dict_datas)
        # return dict_datas

    for key in dict_datas:
        print(dict_datas[key])
        for value in dict_datas[key]:
            print(value)
            print(type(value))

    # return list_datas


@pytest.mark.skip
def testYamlData_2():
    print(getData_2('div'))
    print(type(getData_2('div')))

@pytest.mark.skip
def testReadYamlData():
    print(getYamlData('min'))
    print(type(getYamlData('min')))


class TestCalculator:

    # @pytest.fixture()
    # def initCalculator(self):
    #     print("Init Calculator")
    #     self.cal = Calculator()


    @pytest.fixture(scope="class")
    def initCalculator(self):
        print("Init Calculator")
        yield Calculator()
        print("delete Calculator")


    # #  函数级别
    # def setup(self):
    #     print("TestCalculator setup")
    #     self.cal = Calculator()
    #
    # def teardown(self):
    #     print("TestCalculator teardown")
    #     del self.cal

    # @pytest.mark.parametrize([ "num_A", "num_B", "num_expected"],[[1,2,-1],[0,0,0],[-2,-1,-1],[1.0, 2, -1],['a', 'b',False]] )
    # def testMin(self, initCalculator, num_A, num_B, num_expected):
    #     print(num_A, num_B, num_expected)
    #     assert initCalculator.Min(num_A, num_B) == num_expected
    #
    # @pytest.mark.parametrize(["num_A", "num_B", "num_expected"], [[1, 2, 0.5], [0, 0, "division zero"], [2.0, 1, 2], [-2, -1, 2], ['a', 'b', False]])
    # def testDiv(self, initCalculator,num_A, num_B, num_expected):
    #     assert initCalculator.Div(num_A, num_B) == num_expected


    @pytest.mark.parametrize(["num_A", "num_B", "num_expected"], getData_1('min'))
    def testMin(self, initCalculator, num_A, num_B, num_expected):
        print(num_A, num_B, num_expected)
        logging.info("test min : numA = {}, num_B = {}, expected = {}".format(num_A, num_B, num_expected))
        allure.attach.file(r"C:\Users\zhongkunxia\Pictures\Allure_1.png", attachment_type=allure.attachment_type.JPG)
        assert initCalculator.Min(num_A, num_B) == num_expected

    @pytest.mark.parametrize(["num_A", "num_B", "num_expected"], getData_1('div'))
    def testDiv(self, initCalculator, num_A, num_B, num_expected):
        logging.info("test div : numA = {}, num_B = {}, expected = {}".format(num_A, num_B, num_expected))
        assert initCalculator.Div(num_A, num_B) == num_expected